package com.ttk.issuetracker.vo;

import java.io.Serializable;
import java.util.List;

public class Result<T> implements Serializable {
	private static final long serialVersionUID = 6708665818683449766L;
	final private boolean success;
	final private T data;
	final private String msg;

	Result(boolean success, T data) {
		this.success = success;
		this.data = data;
		this.msg = null;
	}

	Result(boolean success, String msg) {
		this.success = success;
		this.data = null;
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public String getMsg() {
		return msg;
	}

	public T getData() {
		return data;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("\"Result{\"");
		sb.append("success=").append(success);
		sb.append(", msg=").append(msg);
		sb.append(", data=");
		if (data == null) {
			sb.append("null");
		} else if (data instanceof List) {
			List castList = (List) data;
			if (castList.isEmpty()) {
				sb.append("empty list");
			} else {
				Object firstItem = castList.get(0);
				sb.append("List of ").append(firstItem.getClass());
			}
		} else {
			sb.append(data.toString());
		}
		sb.append("}");
		return sb.toString();
	}
}
